﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XBoshiGFS.forms
{
    public partial class VillageForm : Form
    {

        public VillageSet village { get; set; }
        public XBoshiGFSModelContainer context = new XBoshiGFSModelContainer();  

        public VillageForm()
        {
            InitializeComponent();

            village = new VillageSet();
            village.Id = Guid.NewGuid().ToString();
            village.strName = "新建村";
            context.AddToVillageSet(village);
            villageSetBindingSource.DataSource = village;
            cmbStation.DataSource = (from p in context.StationSet select p);


        }

        public VillageForm(WaterCourseSet  p_waterCursor)
        {
            InitializeComponent();


            var waterCursor = (from p in context.WaterCourseSet where p.Id == p_waterCursor.Id select p).FirstOrDefault();
            village = new VillageSet();
            village.WaterCourseSet = waterCursor; 
            village.Id = Guid.NewGuid().ToString();
            village.strName = "新建村";
            context.AddToVillageSet(village);
            villageSetBindingSource.DataSource = village;
   


            var watercourse = village.WaterCourseSet;
            cmbWaterCourse.DataSource = new List<Object>() { watercourse };
            cmbWaterCourse.Enabled = false;
            cmbWaterCourse.SelectedValue = watercourse.Id;

            var segment = watercourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;

            //  var station = segment.StationSet; 
            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }


        public VillageForm(VillageSet p_village)
        {
            InitializeComponent();


            village = (from p in context.VillageSet where p.Id == p_village.Id select p).FirstOrDefault();
            villageSetBindingSource.DataSource = village;
            context.Attach(village);


            var watercourse = village.WaterCourseSet;
            cmbWaterCourse.DataSource = new List<Object>() { watercourse };
            cmbWaterCourse.Enabled = false;
            cmbWaterCourse.SelectedValue = watercourse.Id;

            var segment = watercourse.SegmentCourseSet;
            cmbSegment.DataSource = new List<Object>() { segment };
            cmbSegment.Enabled = false;
            cmbSegment.SelectedValue = segment.Id;


            cmbStation.DataSource = (from p in context.StationSet select p);
            cmbStation.Enabled = false;
        }





        private void btnOK_Click(object sender, EventArgs e)
        {
            if (TBName.Text == "")
            {
                MessageBox.Show("请填写名称！", "提示");
                TBName.Focus();
                return;
            }
            if (TBCode.Text == "")
            {
                MessageBox.Show("请填写代码！", "提示");
                TBCode.Focus();
                return;
            }

            if (String.IsNullOrWhiteSpace(village.WaterCourseId)) {
                MessageBox.Show("请选择上层单位！", "提示");
                return;
            }




 
            var code = (from p in context.VillageSet where p.strCode == TBCode.Text select p);
            var maxCode = "";
            try
            {
                var sugesstNo = (from p in context.VillageSet select p.strCode).Cast<Int32>().Max();
                maxCode = String.Format("{0}", (sugesstNo + 1)); //(int.Parse(sugest.FirstOrDefault().ToString()) + 1).ToString();
            }
            catch
            {
            } 
            if (code.Count() == 0)
            {
            }
            else
            {
                if (code.Count() == 1 && code.First().Id == village.Id)
                {
                }
                else
                {
                    MessageBox.Show("代码重复！建议编码为：" + maxCode, "提示");
                    TBCode.Focus();
                    return;
                }
            }
                //设置sc的相关信息
            if (village.EntityKey == null)
            {

                context.VillageSet.AddObject(village);
            }
            else
            {
             //   segment.Id = Guid.NewGuid().ToString(); 
             //    context.SegmentCourseSet.Attach(segment);
            }
            context.SaveChanges();
            this.DialogResult = DialogResult.Yes; 
        }

        private void VillageForm_Load(object sender, EventArgs e)
        {

        }
        private void cmbStation_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbSegment.DataSource = (from p in context.SegmentCourseSet where p.StationId == strID orderby p.strName select p);
        }

        private void cmbSegment_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            cmbWaterCourse.DataSource = (from p in context.WaterCourseSet where p.SegmentCourseId == strID orderby p.strName select p);
        }

        private void cmbWaterCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            var strID = ((ComboBox)sender).SelectedValue;
            village.WaterCourseId  = strID.ToString(); 
        }

    }
}
